//
//  ProductCommentGetMore.m
//  FloatClouds
//
//  Created by 黄威 on 16/9/10.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ProductCommentGetMore.h"
#import "ProductDetailCell4.h"
#import "HWLNetworkTool.h"
#import "GetMoreCommentModel.h"
#import "Coments.h"
#import "ProductDetailCell5.h"
#import "ProductMyComment.h"

@interface ProductCommentGetMore ()

@property (nonatomic, retain) GetMoreCommentModel *productModel;
@property (nonatomic, assign) CGSize widthAndHeight;

@end

@implementation ProductCommentGetMore

-(void)viewDidLoad{

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerClass:[ProductDetailCell4 class] forCellReuseIdentifier:@"ProductDetailCell4"];
    [self.tableView registerClass:[ProductDetailCell5 class] forCellReuseIdentifier:@"ProductDetailCell5"];
    
    [self createNavigationItem];
    [self handleData];


}

- (void)createNavigationItem{
    
    
    //设置navigation相关属性
    

    
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"全部评论";
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
}


- (void)handleData{
    
    /*ID=553&latitude=38.889637&ln=10&longitude=121.550987&openudid=5ae325c9ecbdeb16530b2ce91cf26c9ad98068bb&sn=0*/
    
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    
    NSString *ln = @"&ln = 999";
    NSString *body = [[@"ID="stringByAppendingString:self.productId]stringByAppendingString:ln];
    
    
    [HWLNetworkTool POST:@"http://app.yanyue.cn/product/getcomment" andBody:body andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
        
        
        self.productModel = [GetMoreCommentModel modelWithDic:result];
        
        [self.tableView reloadData];
        
        
    } andFailureBlock:^(NSError *error) {
    
    }];
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return self.productModel.coments.count;
    }if (section == 1) {
        return 1;
    }

    return 0;
}





-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0 ) {
        ProductDetailCell4 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProductDetailCell4" forIndexPath:indexPath];
        
        cell.name.text = self.productModel.coments[indexPath.row].name;
        cell.time.text = self.productModel.coments[indexPath.row].time;
        cell.landname.text = self.productModel.coments[indexPath.row].province;
        cell.priceInput.text = self.productModel.coments[indexPath.row].local_price;
        cell.content.text = self.productModel.coments[indexPath.row].content;
        
        [self height:self.productModel.coments[indexPath.row].content];
        cell.widthAndHeight = self.widthAndHeight;
        
        return cell;

    }if (indexPath.section == 1) {
        
        ProductDetailCell5 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProductDetailCell5" forIndexPath:indexPath];
        
        [cell.button addTarget:self action:@selector(say) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
        
    }
    
    
    return nil;
    
}
- (void)height:(NSString *)str{
    
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:17],NSFontAttributeName, nil];
    
    UILabel *label = [[UILabel alloc]init];
    label.numberOfLines = 0;
    label.text = str;
    
    self.widthAndHeight = [label.text boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0 ) {
        return self.widthAndHeight.height + 90;
    }
    if (indexPath.section == 1) {
        
        return 60;
    }
    return 0;
}

- (void)say{

    ProductMyComment *myComment = [[ProductMyComment alloc]init];
    
    myComment.productID = self.productId;
    
    [self.navigationController pushViewController:myComment animated:YES];
}
@end
